package eswc.cleaner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.javadocmd.simplelatlng.LatLngTool;
import com.javadocmd.simplelatlng.util.LengthUnit;

public class CheckinUtil {
	private static final int CHECKIN_RADIUS = 50000;

	public static Map<Affiliation, Integer> getCheckins(List<Affiliation> affs,
			List<CoordinateMark> checkins) {
		Map<Affiliation, Integer> res = new HashMap<Affiliation, Integer>();

		for (Affiliation aff : affs) {
			
			if (aff.getCoords()!=null)
			for (CoordinateMark checkin : checkins) {
				if (LatLngTool.distance(aff.getCoords(),
						checkin.getCoordinates(), LengthUnit.METER) <= CHECKIN_RADIUS) {
					if (!res.containsKey(aff))
						res.put(aff, 0);
					res.put(aff, res.get(aff) + 1);
				}
			}
		}

		return res;
	}

	/**
	 * calcualtes the relevance for all affiliations
	 * @param res
	 */
	public static void setRelevance(Map<Affiliation, Integer> res) {
		Integer totalCheckins = totalCheckins = getTotalCheckins(res);

		for (Affiliation a : res.keySet()) {
			a.setRelevance(res.get(a) / totalCheckins.floatValue() * 100f);
		}
	}

	public static int getTotalCheckins(Map<Affiliation, Integer> res) {
		int totalCheckins = 0;
		for (final Affiliation a : res.keySet()) {
			totalCheckins += res.get(a);
		}
		return totalCheckins;
	}
}
